#ifdef WIN32
	#include <WINSOCK2.H>
	#pragma comment(lib,"ws2_32")
#endif
#include "hdr.h"
#include "http.h"

static sock_t g_sock_fd ;

int init_server()
{
	#ifdef WIN32
	{
		WSADATA data;
		WSAStartup(0x0202, &data);
	}
	#endif
	{
		struct sockaddr_in addr;
		addr.sin_addr.s_addr   = ADDR_ANY;
		addr.sin_family = AF_INET;
		addr.sin_port   = htons(HTTP_PORT);
		
		g_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
		bind(g_sock_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
		listen(g_sock_fd,20);
	}
	return R_OK;
}

int http_accept()
{
	char buff[1024] = {0};
	sock_t rmt_sock;
	struct sockaddr_in remote_addr;
	int addr_size;
	
	
	addr_size = sizeof(struct sockaddr_in);
	while(rmt_sock = accept(g_sock_fd, (struct sockaddr*)&remote_addr, &addr_size))
	{
#define HTTP_200 "HTTP/1.1 200 OK\r\nSet-Cookie: ISESSION_ID=147852369; path=/\r\n\r\n\r\n<form action='' method='post'><input type='text' name='username' /><br><input type='text' name='password' /><input type='submit' value='submit' /></form>\n\n"
		PT("THIS IS A ETS\n");
		
		memset(buff, 0, 1024);
		recv(rmt_sock, buff, 1024, 0);
		{
			//test
			http_main(buff);
			
		 

		}

		send(rmt_sock, HTTP_200,strlen(HTTP_200), 0);
		closesocket(rmt_sock);
	}

	return R_OK;
}

